package com.company.observer.listener.message;

import com.company.observer.listener.EvenListener;
import com.company.observer.mode.Result;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author : hxma@easipass.com
 * @Date : 2021/5/11
 **/
public class EventManager {

//    private Map<Enum<EnumType>, List<EvenListener>> listeners = new HashMap<>();  //效果一样
    private Map<EnumType, List<EvenListener>> listeners = new HashMap<>();

     public enum EnumType{
         MQ,
         MESSAGE
    }

//    public EventManager(Enum<EnumType>... operators) {
//        for (Enum<EnumType> operator:operators){
//            listeners.put(operator,new ArrayList<>());
//        }
//    }

    public EventManager(EnumType... opes){
         for (EnumType ope: opes){
             listeners.put(ope,new ArrayList<>());
         }
    }

    public void subscribe(EnumType type,EvenListener listener){
        List<EvenListener> evenListeners = listeners.get(type);
        evenListeners.add(listener);
    }

    public void enSubscribe(EnumType type,EvenListener listener){
        List<EvenListener> evenListeners = listeners.get(type);
        evenListeners.remove(listener);
    }

    public void notifyMessage(EnumType type, Result result){
        List<EvenListener> list = listeners.get(type);
        for (EvenListener evenListener : list){
            evenListener.doEvent(result);
        }
    }
}
